package com.tencent.sr.iris.activity.service.event.task;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.activity.common.enums.TaskRecordAwardIssueStatusEnum;
import com.tencent.sr.iris.activity.common.enums.UserTaskStatusEnum;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.repository.entity.TIrisActivityAwardRecordDO;
import com.tencent.sr.iris.activity.repository.entity.TIrisUserTaskRecordDO;
import com.tencent.sr.iris.activity.service.activity.ActivityCommonService;
import com.tencent.sr.iris.activity.service.activity.IrisUserTaskRecordService;
import com.tencent.sr.iris.activity.service.event.vo.BaseUserTaskDataVO;
import com.tencent.sr.iris.activity.service.event.vo.TaskExecutorExtendInfo;
import java.time.LocalDate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 描述用途
 *
 * @author wruiiwang
 * @date 2023/2/17
 */
@Slf4j
@Service
public class UserLevelUpFosterTaskExecutor extends AbstractIrisTaskExecutor {

    @Autowired
    private IrisUserTaskRecordService userTaskRecordService;

    @Autowired
    private ActivityCommonService activityCommonService;

    @Override
    public void process(ActivityEventDTO msg, ActivityDTO activity, ActivityTaskDTO task,
            List<String> eventHandingInfoList, TaskExecutorExtendInfo taskExecutorExtendInfo) {
        // 查询任务完成进度
        TIrisUserTaskRecordDO userTaskRecordDO = queryUserTaskRecord(msg.getUid(), activity.getBizId(),
                task.getBizId(), null);
        log.info("升级有礼(培育奖)事件处理，当前任务完成进度UserLevelUpFosterTaskExecutor:{}", JSON.toJSONString(userTaskRecordDO));
        if (Objects.nonNull(userTaskRecordDO)) {
            if (Objects.equals(TaskRecordAwardIssueStatusEnum.ISSUE_SUCCEED.getCode(),
                    userTaskRecordDO.getAwardIssueStatus())) {
                log.info("升级有礼(培育奖)，下级用户已完成任务UserLevelUpFosterTaskExecutor,uid:{},ActivityEventDTO:{},", msg.getUid(),
                        JSON.toJSONString(msg));
                return;
            }
        } else {
            //新增用户活动任务完成进度
            userTaskRecordDO = buildUserTaskRecordDO(activity, task, msg);
            log.info("升级有礼(培育奖)任务达成UserLevelUpFosterTaskExecutor,uid:{},ActivityEventDTO:{}", msg.getUid(),
                    JSON.toJSONString(msg));
        }
        //发放权益
        TIrisActivityAwardRecordDO awardRecordDO = activityCommonService.sendAwardForSupervisor(userTaskRecordDO,
                task);
        //记录任务进度
        userTaskRecordService.updateUserTaskRecordStatus(userTaskRecordDO, awardRecordDO);

    }

    @Override
    public void fillTaskRecordStatus(ActivityEventDTO eventDTO, ActivityDTO act, ActivityTaskDTO task,
            TIrisUserTaskRecordDO userTaskRecordDO, LocalDate currentLocalDate) {
    }


    @Override
    public boolean executeAward(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task) {
        return false;
    }

    @Override
    public boolean checkTaskThreshold(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task,
            BaseUserTaskDataVO userTaskDataVO) {
        return false;
    }

    @Override
    public BaseUserTaskDataVO prepareUserTaskData(ActivityEventDTO event, ActivityDTO act, ActivityTaskDTO task) {
        return null;
    }
}
